﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using jsm.SqlHelper;

namespace dbcaac
{
    public sealed class EntityJiyuanNR : EntityBase
    {
        public string jyId { get; private set; }
        public string jyName { get; private set; }
        public string jyContent { get; private set; }
        public string jyStandard { get; private set; }
        public string jyBasis { get; private set; }
        public string jyPenalizeRef { get; private set; }
        public string jyParentId { get; private set; }
        public string jyProjectId { get; private set; }
        public string jyCreateOwner { get; private set; }
        public string jyIsMust { get; private set; }
        public string jyIsTemp { get; private set; }

        public string showTemp { get; private set; }

        public override string getIdField() { return "jyId"; }
        public override string getPidField() { return "jyParentId"; }


        public override void loadPageData(HttpContext context)
        {
            jyName = context.Request["jyName"];
            jyContent = context.Request["jyContent"];
            jyStandard = context.Request["jyStandard"];
            jyBasis = context.Request["jyBasis"];
            jyPenalizeRef = context.Request["jyPenalizeRef"];
            jyParentId = context.Request["pid"];
            if (string.IsNullOrEmpty(jyParentId))
            {
                jyParentId = context.Request["jyParentId"];
            }
            jyProjectId = context.Request["project"];
            if (string.IsNullOrEmpty(jyProjectId))
            {
                jyProjectId = context.Request["jyProjectId"];
            }
            jyCreateOwner = "1";    // TODO: 从Session中取用户
            jyIsMust = context.Request["jyIsMust"];
            jyIsTemp = "0";
            jyId = context.Request["id"];
            if (string.IsNullOrEmpty(jyId))
            {
                jyId = context.Request["jyId"];
            }
            if (string.IsNullOrEmpty(jyId))
            {
                int maxId = (int)SqlServerHelper.ExecuteScalar("SELECT MAX(jyId) FROM jiyuanNR");
                jyId = (++maxId).ToString();
            }
            showTemp = context.Request["showTemp"];
        }

        public override string isValid()
        {
            if (string.IsNullOrEmpty(jyName))
                return "检查内容不能为空";
            if (string.IsNullOrEmpty(jyId))
                return "未知错误，请重试或联系管理员";
            return null;
        }

        protected override int doInsertOperation()
        {
            using (SqlServerHelper helper = new SqlServerHelper())
            {
                helper.Open();
                SqlTransaction transaction = helper.Connection.BeginTransaction();
                helper.Command.Transaction = transaction;
                try
                {
                    // 1、插入数据
                    helper.Command.CommandText = @"INSERT INTO [jiyuanNR] (jyId, jyName, jyContent, jyStandard, jyBasis, jyPenalizeRef, jyParentId, 
                                                    jyProjectId, jyCreateOwner, jyCreateDate, jyIsMust, jyIsTemp, jyNewFlag, jyLogId)
                                                    VALUES (@jyId, @jyName, @jyContent, @jyStandard, @jyBasis, @jyPenalizeRef,
                                                    @jyParentId, @jyProjectId, @jyCreateOwner, @jyCreateDate, @jyIsMust, @jyIsTemp, 1, @jyLogId)";
                    Guid logId = Guid.NewGuid();
                    string time = System.DateTime.Now.ToString();

                    helper.AddParameter("@jyId", SqlDbType.Int, int.Parse(jyId));
                    helper.AddParameter("@jyName", SqlDbType.NVarChar, jyName);
                    helper.AddParameter("@jyContent", SqlDbType.NText, jyContent);
                    helper.AddParameter("@jyStandard", SqlDbType.NText, jyStandard);
                    helper.AddParameter("@jyBasis", SqlDbType.NText, jyBasis);
                    helper.AddParameter("@jyPenalizeRef", SqlDbType.NText, jyPenalizeRef);
                    helper.AddParameter("@jyParentId", SqlDbType.Int, jyParentId);
                    helper.AddParameter("@jyProjectId", SqlDbType.Int, jyProjectId);
                    helper.AddParameter("@jyCreateOwner", SqlDbType.Int, jyCreateOwner);
                    helper.AddParameter("@jyCreateDate", SqlDbType.DateTime, time);
                    helper.AddParameter("@jyIsMust", SqlDbType.Bit, int.Parse(jyIsMust));
                    helper.AddParameter("@jyIsTemp", SqlDbType.Bit, int.Parse(jyIsTemp));
                    helper.AddParameter("@jyLogId", SqlDbType.UniqueIdentifier, logId);

                    int retVal = helper.ExecuteNoneQuery();

                    // 2、写日志
                    helper.Command.CommandText =
                        @"INSERT INTO [jiyuanNRLog] (jyrzLogId, jyrzChangedId, jyrzChangedType, jyrzChangedBy, jyrzChangedDate, jyrzApprovalStatus,
                        jyrzName, jyrzContent, jyrzStandard, jyrzBasis, jyrzPenalizeRef, jyrzParentId, jyrzProjectId, jyrzIsMust, jyrzIsTemp)
                        VALUES (@jyrzLogId, @jyrzChangedId, @jyrzChangedType, @jyrzChangedBy, @jyrzChangedDate, @jyrzApprovalStatus,
                        @jyrzName, @jyrzContent, @jyrzStandard, @jyrzBasis, @jyrzPenalizeRef, @jyrzParentId, @jyrzProjectId, @jyrzIsMust, @jyrzIsTemp)";

                    helper.AddParameter("@jyrzLogId", SqlDbType.UniqueIdentifier, logId);
                    helper.AddParameter("@jyrzChangedId", SqlDbType.Int, int.Parse(jyId));
                    helper.AddParameter("@jyrzChangedType", SqlDbType.VarChar, "NEW");
                    helper.AddParameter("@jyrzChangedBy", SqlDbType.Int, 1);    // TODO: 取用户id
                    helper.AddParameter("@jyrzChangedDate", SqlDbType.DateTime, time);
                    helper.AddParameter("@jyrzApprovalStatus", SqlDbType.VarChar, "UNAPPROVED");

                    helper.AddParameter("@jyrzName", SqlDbType.NVarChar, jyName);
                    helper.AddParameter("@jyrzContent", SqlDbType.NText, jyContent);
                    helper.AddParameter("@jyrzStandard", SqlDbType.NText, jyStandard);
                    helper.AddParameter("@jyrzBasis", SqlDbType.NText, jyBasis);
                    helper.AddParameter("@jyrzPenalizeRef", SqlDbType.NText, jyPenalizeRef);
                    helper.AddParameter("@jyrzParentId", SqlDbType.Int, jyParentId);
                    helper.AddParameter("@jyrzProjectId", SqlDbType.Int, jyProjectId);
                    helper.AddParameter("@jyrzIsMust", SqlDbType.Bit, int.Parse(jyIsMust));
                    helper.AddParameter("@jyrzIsTemp", SqlDbType.Bit, int.Parse(jyIsTemp));

                    helper.ExecuteNoneQuery();

                    transaction.Commit();
                    return retVal;
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
            } 
        }

        protected override int doDeleteOperation()
        {
            string sqlText = @"INSERT INTO [jiyuanNRLog] (jyrzLogId, jyrzChangedId, jyrzChangedType, jyrzChangedBy, jyrzChangedDate, jyrzApprovalStatus)
                               VALUES (@jyrzLogId, @jyrzChangedId, @jyrzChangedType, @jyrzChangedBy, @jyrzChangedDate, @jyrzApprovalStatus)";

            List<SqlParameter> sqlParams = new List<SqlParameter>();

            sqlParams.Add(new SqlParameter("@jyrzLogId", Guid.NewGuid()));
            sqlParams.Add(new SqlParameter("@jyrzChangedId", int.Parse(jyId)));
            sqlParams.Add(new SqlParameter("@jyrzChangedType", "DELETE"));
            sqlParams.Add(new SqlParameter("@jyrzChangedBy", "1"));    // TODO: 取用户id
            sqlParams.Add(new SqlParameter("@jyrzChangedDate", System.DateTime.Now.ToString()));
            sqlParams.Add(new SqlParameter("@jyrzApprovalStatus", "UNAPPROVED"));

            return SqlServerHelper.ExecuteNonQuery(sqlText, sqlParams.ToArray());
        }

        protected override int doUpdateOperation()
        {
            string sqlText = @"INSERT INTO [jiyuanNRLog] (jyrzLogId, jyrzChangedId, jyrzChangedType, jyrzChangedBy, jyrzChangedDate, jyrzApprovalStatus,
                                jyrzName, jyrzContent, jyrzStandard, jyrzBasis, jyrzPenalizeRef, jyrzIsMust, jyrzIsTemp)
                                VALUES (@jyrzLogId, @jyrzChangedId, @jyrzChangedType, @jyrzChangedBy, @jyrzChangedDate, @jyrzApprovalStatus,
                                @jyrzName, @jyrzContent, @jyrzStandard, @jyrzBasis, @jyrzPenalizeRef, @jyrzIsMust, @jyrzIsTemp)";

            List<SqlParameter> sqlParams = new List<SqlParameter>();

            sqlParams.Add(new SqlParameter("@jyrzLogId", Guid.NewGuid()));
            sqlParams.Add(new SqlParameter("@jyrzChangedId", int.Parse(jyId)));
            sqlParams.Add(new SqlParameter("@jyrzChangedType", "UPDATE"));
            sqlParams.Add(new SqlParameter("@jyrzChangedBy", "1"));    // TODO: 取用户id
            sqlParams.Add(new SqlParameter("@jyrzChangedDate", System.DateTime.Now.ToString()));
            sqlParams.Add(new SqlParameter("@jyrzApprovalStatus", "UNAPPROVED"));

            sqlParams.Add(new SqlParameter("@jyrzName", jyName));
            sqlParams.Add(new SqlParameter("@jyrzContent", jyContent));
            sqlParams.Add(new SqlParameter("@jyrzStandard", jyStandard));
            sqlParams.Add(new SqlParameter("@jyrzBasis", jyBasis));
            sqlParams.Add(new SqlParameter("@jyrzPenalizeRef", jyPenalizeRef));
            sqlParams.Add(new SqlParameter("@jyrzIsMust", jyIsMust));
            sqlParams.Add(new SqlParameter("@jyrzIsTemp", jyIsTemp));

            return SqlServerHelper.ExecuteNonQuery(sqlText, sqlParams.ToArray());
        }

        public override SqlDataReader executeDataReader()
        {
            string sqlText = null;
            List<SqlParameter> sqlParams = new List<SqlParameter>();

            string optionField = string.IsNullOrEmpty(showTemp) ? "jyIsTemp AS dummyField" : "jyIsTemp AS jyIsTempVar";

            if (string.IsNullOrEmpty(jyProjectId))
            {
                sqlText = string.Format(@"SELECT jyId, jyName, jyContent, jyStandard, jyBasis, jyPenalizeRef, jyParentId, jyProjectId, jyIsTemp, {0},
                            (SELECT nbyhName from [neibuYH] WHERE nbyhId = aTbl.jyCreateOwner), 
                            CONVERT(VARCHAR(100), jyCreateDate, 23) AS jyCreateDate, 
                            (CASE WHEN jyIsMust=1 THEN '是' ELSE '否' END) AS jyIsMust 
                            FROM [jiyuanNR] AS aTbl WHERE (jyNewFlag <> 1) AND (jyDeleteFlag <> 1)", optionField);
            }
            else
            {
                sqlText = string.Format(@"SELECT jyId, jyName, jyContent, jyStandard, jyBasis, jyPenalizeRef, jyParentId, jyProjectId, jyIsTemp, {0},
                            (SELECT nbyhName from [neibuYH] WHERE nbyhId = aTbl.jyCreateOwner), 
                            CONVERT(VARCHAR(100), jyCreateDate, 23) AS jyCreateDate, 
                            (CASE WHEN jyIsMust=1 THEN '是' ELSE '否' END) AS jyIsMust 
                            FROM [jiyuanNR] AS aTbl WHERE (jyProjectId = @jyProjectId AND jyNewFlag <> 1 AND jyDeleteFlag <> 1)", optionField);
                sqlParams.Add(new SqlParameter("@jyProjectId", jyProjectId));
            }

            return SqlServerHelper.ExecuteReader(sqlText, sqlParams.ToArray());
        }
    }
}
